package com.wondersgroup.fjzlpt.plat.dao.impl;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.quick.framework.boot.repository.sql.bean.SQLBean;
import com.quick.framework.boot.repository.sql.jdbcTemplate.impl.BaseJTDAOImpl;
import com.wondersgroup.fjzlpt.plat.dao.inter.LoginDAO;
import org.springframework.stereotype.Repository;

/**
 * Created by lion on 2017/5/21.
 */
@Repository
public class LoginDAOImpl extends BaseJTDAOImpl implements LoginDAO {

	/**
	 * 查询用户唯一ID
	 * 
	 * @param username
	 * @param password
	 * @return
	 */
	public String queryUserUniqueId(String username, String password) {
		StringBuilder sbud = new StringBuilder(
				"select USERID from tb_auth_user where 1=1 and username=? and password=? and STATE='1'");
		SQLBean sb = new SQLBean(sbud.toString(), new Object[] { username,
				password });
		JSONObject json = this.queryOne(sb);
		return json.getString("USERID");
	}

	/**
	 * 查询用户信息
	 * 
	 * @param userUniqueId
	 * @return
	 */
	public JSONObject queryUserInfo(String userUniqueId) {
		StringBuilder sbud = new StringBuilder(
				"select * from view_yljgryxx where USERID=?");
		SQLBean sb = new SQLBean(sbud.toString(), new Object[] { userUniqueId });
		return this.queryOne(sb);
	}

	/**
	 * 查询用户对应的角色信息
	 * 
	 * @param userUniqueId
	 * @return
	 */
	public JSONObject queryUserRoleInfo(String userUniqueId) {
		JSONObject roleInfo = queryRoleInfoByUserUniqueId(userUniqueId);
		String roleId = roleInfo.getString("ROLEID");
		JSONArray menu = queryRoleMenu(roleId);
		JSONArray button = queryMenuButton(roleId);
		roleInfo.put("MENUS", menu);
		roleInfo.put("BUTTONS", button);
		return roleInfo;
	}

	/**
	 * 查询指定的角色信息
	 * 
	 * @param roleId
	 * @return
	 */
	public JSONObject queryRoleInfo(String roleId) {
		JSONObject roleInfo = queryRoleInfoByRoleId(roleId);
		JSONArray menu = queryRoleMenu(roleId);
		roleInfo.put("MENUS", menu);
		return roleInfo;
	}

	private JSONObject queryRoleInfoByRoleId(String roleId) {
		StringBuilder sbud = new StringBuilder(
				"select roleid, rolename from tb_auth_role where roleid=?");
		SQLBean sb = new SQLBean(sbud.toString(), new Object[] { roleId });
		return this.queryOne(sb);
	}

	private JSONObject queryRoleInfoByUserUniqueId(String userUniqueId) {
		StringBuilder sbud = new StringBuilder(
				"select a.roleid, a.rolename from tb_auth_role a, tb_auth_user_role b where 1=1 \n")
				.append("and a.roleid=b.roleid and b.userid=?");
		SQLBean sb = new SQLBean(sbud.toString(), new Object[] { userUniqueId });
		return this.queryOne(sb);
	}

	/**
	 * 查询角色菜单信息
	 * 
	 * @param roleId
	 * @return
	 */
	private JSONArray queryRoleMenu(String roleId) {
		JSONObject roleInfo = new JSONObject();
		StringBuilder sbud = new StringBuilder(
				"select a.MENUID, a.MENUNAME, a.VIEWURL, a.PRE_MENUID, a.MENU_LEVEL, b.NAVIGATE \n")
				.append("from tb_auth_menu a, tb_auth_role_menu b  \n")
				.append("where 1=1 and a.menuid=b.menuid and b.roleid=? order by MENU_ORDER,MENUID \n");
		SQLBean sb = new SQLBean(sbud.toString(), new Object[] { roleId });
		return this.queryList(sb);
	}

	/**
	 * 查询角色菜单对应的按钮
	 * @return
	 */
	private JSONArray queryMenuButton(String roleId) {
		StringBuilder sbud = new StringBuilder(" select b.viewurl, a.CONTROL_IDS from tb_auth_role_menu_action a ,tb_auth_menu b \n" )
				.append(" where a.menuid = b.menuid and a.roleid = ?  \n");
		SQLBean sb = new SQLBean(sbud.toString(), new Object[] { roleId });
		return this.queryList(sb);
	}

	/**
	 * 当前用户是否是团队长
	 * 
	 * @param userUniqueId
	 * @return
	 */
	public boolean isTdz(String userUniqueId) {
		StringBuilder sbud = new StringBuilder(
				"select count(1) from tb_tdgl_tdry where tdjs='1' and userid=?");
		SQLBean sb = new SQLBean(sbud.toString(), new Object[] { userUniqueId });
		return this.isExist(sb);
	}
}
